// Implementation of the ClientSocket class

#include "ClientSocket.h"
#include "SocketException.h"

using namespace std;

ClientSocket::ClientSocket(string host, int port )
{
  if ( ! Socket::create() )
    {
      throw SocketException ( "Impossible de créer le socket du client." );
    }

  if ( ! Socket::connect ( host, port ) )
    {
      throw SocketException ( "Could not bind to port." );
    }

}


const ClientSocket& ClientSocket::operator << ( const string& s ) const{

	if(!Socket::send (s)){
	   	throw SocketException ( "Could not write a string to socket." );
    }

	return *this;

}

const ClientSocket& ClientSocket::operator << ( const int& n ) const{
	
	if (!Socket::send (n)){
		throw SocketException ( "Could not write an int to socket." );
    }

	return *this;
}


const ClientSocket& ClientSocket::operator >> (string& s ) const{

	if(!Socket::recv(s)){
    	throw SocketException ( "Could not read a string from socket." );
    }

	return *this;
}

const ClientSocket& ClientSocket::operator >> (int& n) const{

  if ( ! Socket::recv (n) )
    {
      throw SocketException ( "Could not read an int from socket." );
    }

  return *this;
}

